package com.nuoniu.sibanyun.controller.so;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.so.VSaleDeliveryMain;
import com.nuoniu.sibanyun.service.so.IVSaleDeliveryMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 销售发货单主表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/so/deliveryMain")
@Api(value="销售发货单管理", tags={"销售发货单管理"})
public class VSaleDeliveryMainController extends BaseController {

    @Autowired
    private IVSaleDeliveryMainService saleDeliveryMainService;

    @ApiOperation("获取单据编号")
    @GetMapping("getCode")
    public HttpResult getCode() {
        return HttpResult.ok(saleDeliveryMainService.getCode());
    }

    @ApiOperation("保存发货单")
    @PostMapping("save")
    public HttpResult save(@RequestBody VSaleDeliveryMain saleDeliveryMain) {
        if(saleDeliveryMain == null || saleDeliveryMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("数据为空");
        }

        saleDeliveryMainService.saveDelivery(saleDeliveryMain);
        return HttpResult.ok();
    }

    @ApiOperation("保存发货单(根据仓库 分单)")
    @PostMapping("saveOrderForWarehouse")
    public HttpResult saveOrderForWarehouse(@RequestBody VSaleDeliveryMain saleDeliveryMain) {
        if(saleDeliveryMain == null || saleDeliveryMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("数据为空");
        }

        saleDeliveryMainService.saveOrderForWarehouse(saleDeliveryMain);
        return HttpResult.ok();
    }

    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        saleDeliveryMainService.submit(id, status);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        saleDeliveryMainService.examine(id, status);
        return HttpResult.ok();
    }

    @GetMapping("apply")
    @ApiOperation("提交申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 1:退单,2:提交申请")
    })
    public HttpResult apply(Long id, Integer status) {
        saleDeliveryMainService.apply(id,status);
        return HttpResult.ok();
    }


    @ApiOperation("删除单据")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        saleDeliveryMainService.deleteById(id);
        return HttpResult.ok();
    }
}
